<?php

/*
 +--------------------------------------------------------------------+
 | CiviCRM version 3.3                                               |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2010                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/**
 *
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2010
 * $Id$
 *
 */

require_once 'CRM/Event/Form/Registration.php';

/**
 * This class generates form components for processing Event  
 * 
 */
class CRM_Event_Form_Registration_ParticipantConfirm extends CRM_Event_Form_Registration
{
    // optional credit card return status code
    // CRM-6060
    protected $_cc = null;

    /** 
     * Function to set variables up before form is built 
     *                                                           
     * @return void 
     * @access public 
     */ 
    public function preProcess( ) 
    {
        $this->_participantId = CRM_Utils_Request::retrieve( 'participantId', 'Positive', $this );
        
        $this->_cc = CRM_Utils_Request::retrieve( 'cc', 'String', $this );

        //get the contact and event id and assing to session.
        $values = array( );
        $csContactID = $eventId = null;
        if ( $this->_participantId ) {
            require_once 'CRM/Event/BAO/Participant.php';
            $params = array('id' => $this->_participantId );
            CRM_Core_DAO::commonRetrieve( 'CRM_Event_DAO_Participant', $params, $values, 
                                          array( 'contact_id', 'event_id', 'status_id' ) );
        }
        
        $this->_participantStatusId = $values['status_id'];
        $this->_eventId = CRM_Utils_Array::value( 'event_id', $values );
        $csContactId    = CRM_Utils_Array::value( 'contact_id', $values );
        
        // make sure we have right permission to edit this user
        $this->_csContactID = null;
        if ( $csContactId && $this->_eventId ) {
            $session = CRM_Core_Session::singleton( );
            if ( $csContactId == $session->get( 'userID' ) ) {
                $this->_csContactID = $csContactId;
            } else {
                require_once 'CRM/Contact/BAO/Contact/Permission.php';
                if ( CRM_Contact_BAO_Contact_Permission::validateChecksumContact( $csContactId, $this ) ) {
                    //since we have landing page so get this contact
                    //id in session if user really want to walk wizard.
                    $this->_csContactID = $csContactId;
                }
            }
        }
        
        if ( !$this->_csContactID ) {
            $config = CRM_Core_Config::singleton( );
            CRM_Core_Error::statusBounce( ts( 'You do not have permission to access this event registration. Contact the site administrator if you need assistance.' ),$config->userFrameworkBaseURL );
        }
    }
    
    /** 
     * Function to build the form 
     * 
     * @return None 
     * @access public 
     */ 
    public function buildQuickForm( )  
    { 
        $params = array('id' => $this->_eventId );
        $values = array( );
        CRM_Core_DAO::commonRetrieve( 'CRM_Event_DAO_Event', $params, $values, 
                                     array( 'title' ) );

        $buttons = array( );
        require_once 'CRM/Event/PseudoConstant.php';
        // only pending status class family able to confirm.
        
        $statusMsg = null;
        if ( array_key_exists( $this->_participantStatusId, 
                               CRM_Event_PseudoConstant::participantStatus( null, "class = 'Pending'" ) ) ) {

            
            //need to confirm that though participant confirming
            //registration - but is there enough space to confirm.
            require_once 'CRM/Event/PseudoConstant.php';
            require_once 'CRM/Event/BAO/Participant.php';
            $emptySeats = CRM_Event_BAO_participant::pendingToConfirmSpaces( $this->_eventId );
            $additonalIds = CRM_Event_BAO_participant::getAdditionalParticipantIds( $this->_participantId );
            $requireSpace = 1 + count( $additonalIds );
            if ( $emptySeats !== null && ( $requireSpace > $emptySeats ) ) {
                $statusMsg =  ts( "Oops, it looks like there are currently no available spaces for the %1 event.", array(1 => $values['title']) );
            } else {
                if ($this->_cc == 'fail') {
                    $statusMsg = '<div class="bold">' . ts( 'Your Credit Card transaction was not successful. No money has yet been charged to your card.') .
                        '</div><div><br />' . ts('Click the "Confirm Registration" button to complete your registration in %1, or click "Cancel Registration" if you are no longer interested in attending this event.', array(1 => $values['title']) ) . '</div>'; 
                } else {
                    $statusMsg = '<div class="bold">' . ts( 'Confirm your registration for %1.', array(1 => $values['title'])) .
                        '</div><div><br />' .  ts('Click the "Confirm Registration" button to begin, or click "Cancel Registration" if you are no longer interested in attending this event.' ) . '</div>';
                }
                $buttons = array_merge( $buttons, array( array( 'type'      => 'next',
                                                                'name'      => ts('Confirm Registration'), 
                                                                'spacing'   => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 
                                                                'isDefault' => true   ))); 
            }
        }
        
        // status class other than Negative should be able to cancel registration.
        if ( array_key_exists( $this->_participantStatusId,
                               CRM_Event_PseudoConstant::participantStatus( null, "class != 'Negative'" ) ) ) {
            $cancelConfirm = ts('Are you sure you want to cancel your registration for this event?');
            $buttons = array_merge( $buttons, array(array( 'type'    => 'submit',
                                                           'name'    => ts('Cancel Registration'),
                                                           'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
                                                           'js'      => array('onclick' => 'return confirm(\'' . $cancelConfirm . '\');'))));
            if ( !$statusMsg ) {
                $statusMsg = ts( 'You can cancel your registration for %1 by clicking "Cancel Registration".' , array(1 => $values['title']) );
            }
        }
        if ( !$statusMsg ) {
            $statusMsg = ts( "Oops, it looks like your registration for %1 has already been cancelled.", 
                             array(1 => $values['title']) );
        }
        $this->assign( 'statusMsg', $statusMsg );
        
        $this->addButtons( $buttons );
    }
    
    /**
     * Function to process the form
     *
     * @access public
     * @return None
     */
    public function postProcess( ) 
    {
        //get the button.
        $buttonName = $this->controller->getButtonName( );
        $eventId = $this->_eventId;
        $participantId = $this->_participantId;
        
        if ( $buttonName == '_qf_ParticipantConfirm_next' ) {
            //lets get contact id in session.
            $session = CRM_Core_Session::singleton( );
            $session->set( 'userID', $this->_csContactID );
            
            //check user registration status is from pending class
            $url = CRM_Utils_System::url( 'civicrm/event/register', "reset=1&id={$eventId}&participantId={$participantId}" );
            CRM_Utils_System::redirect( $url );
        } else if ( $buttonName == '_qf_ParticipantConfirm_submit' ) {
            //need to registration status to 'cancelled'.
            require_once 'CRM/Event/PseudoConstant.php';
            require_once 'CRM/Event/BAO/Participant.php';
            $cancelledId = array_search( 'Cancelled', CRM_Event_PseudoConstant::participantStatus( null, "class = 'Negative'" ) );
            $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds( $participantId );
            
            $participantIds = array_merge( array( $participantId ), $additionalParticipantIds );
            $results = CRM_Event_BAO_Participant::transitionParticipants( $participantIds, $cancelledId, null, true );
            
            if ( count( $participantIds ) > 1 ) {
                $statusMessage = ts( "%1 Event registration(s) have been cancelled.", array( 1 => count( $participantIds ) ) );
            } else {
                $statusMessage = ts( "Your event registration has been cancelled." );
            }
            if ( CRM_Utils_Array::value( 'mailedParticipants', $results ) ) {
                foreach ( $results['mailedParticipants'] as $key => $displayName ) {
                    $statusMessage .=  "<br />" . ts( "Email has been sent to : %1", array( 1 => $displayName ) );
                }
            }
            
            CRM_Core_Error::statusBounce( $statusMessage, CRM_Utils_System::url( 'civicrm/event/info',"reset=1&id={$this->_eventId}&noFullMsg=1",
                                                                                 false, null, false, true ) );
        }
    }
}

